package com.example.banksys;

import DataBase.DBMsg;
import com.pixelduke.control.skin.FXSkins;
import handler.queryHandler;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.sql.SQLException;

public class AccountManagePageController {
    public TableView<Account> resTable;
    public TableColumn<Account,Number> IDCol;
    public TableColumn<Account, String> TypeCol;
    public TableColumn<Account,Number> MoneyCol;
    public TableColumn<Account,String> DateCol;
    public TableColumn<Account, Boolean> EditCol;
    public TextField identity;
    public Button Query;

    public ObservableList<Account> accountObservableList;

    public void initialize(){
        accountObservableList= FXCollections.observableArrayList();
    }

    public void Query() throws SQLException {
        queryHandler queryHandler=new queryHandler();
        DBMsg resMsg=queryHandler.accountManageQuery(identity,accountObservableList);
        if(resMsg.getState()== DBMsg.State.ERROR){
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.titleProperty().set("提示");
            alert.headerTextProperty().set(resMsg.getInfo());
            alert.showAndWait();
        }else{
            resTable.setItems(accountObservableList);
            IDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Account, Number>, ObservableValue<Number>>() {
                @Override
                public ObservableValue<Number> call(TableColumn.CellDataFeatures<Account, Number> accountNumberCellDataFeatures) {
                    return new SimpleLongProperty(accountNumberCellDataFeatures.getValue().getAccount_ID());
                }
            });
            TypeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Account, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<Account, String> accountStringCellDataFeatures) {
                    return new SimpleStringProperty(accountStringCellDataFeatures.getValue().getType());
                }
            });
            MoneyCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Account, Number>, ObservableValue<Number>>() {
                @Override
                public ObservableValue<Number> call(TableColumn.CellDataFeatures<Account, Number> accountNumberCellDataFeatures) {
                    return new SimpleDoubleProperty(accountNumberCellDataFeatures.getValue().getBalance());
                }
            });
            DateCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Account, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<Account, String> accountStringCellDataFeatures) {
                    return new SimpleStringProperty(accountStringCellDataFeatures.getValue().getOpen_time().toString());
                }
            });
            EditCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Account, Boolean>, ObservableValue<Boolean>>() {
                @Override
                public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Account, Boolean> accountBooleanCellDataFeatures) {
                    return new SimpleBooleanProperty(accountBooleanCellDataFeatures.getValue()!=null);
                }
            });
            EditCol.setCellFactory(new Callback<TableColumn<Account, Boolean>, TableCell<Account, Boolean>>() {
                @Override
                public TableCell<Account, Boolean> call(TableColumn<Account, Boolean> accountBooleanTableColumn) {
                    return new TableCell<>(){
                        @Override
                        protected void updateItem(Boolean item, boolean notempty) {
                            super.updateItem(item, notempty);
                            Button button = new Button("管理");
                            button.setOnAction(new EventHandler<ActionEvent>() {
                                @Override
                                public void handle(ActionEvent actionEvent) {
                                    FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("AccountLogin.fxml"));
                                    Scene scene = null;
                                    try {
                                        scene = new Scene(fxmlLoader.load());
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    scene.getStylesheets().add(FXSkins.getStylesheetURL());
                                    AccountLoginController ac=fxmlLoader.getController();
                                    Account emp=accountObservableList.get(getIndex());
                                    ac.account_ID= emp.getAccount_ID();
                                    /*EditController ctrl=fxmlLoader.getController();
                                    Account emp=accountObservableList.get(getIndex());//太关键了，至关重要的一个获得index
                                    ctrl.ID.setText(emp.getEmployeeID().getValue().toString());
                                    ctrl.namestr=emp.getName().get();
                                    ctrl.orgstr=emp.getOrganization().get();
                                    ctrl.depstr=emp.getDepartment().get();
                                    ctrl.typstr=emp.getType().get();
                                    ctrl.name.setText(ctrl.namestr);
                                    ctrl.Department.setText(ctrl.depstr);
                                    ctrl.Organization.setText(ctrl.orgstr);
                                    ctrl.Type.setText(ctrl.typstr);
                                    */
                                    Stage t=new Stage();
                                    t.setScene(scene);
                                    t.show();
                                }
                            });
                            if (notempty) {
                                //如果此列为空默认不添加元素
                                setText(null);
                                setGraphic(null);
                            } else {
                                this.setGraphic(button);
                            }
                        }
                    };
                }
            });
        }
    }
}
